package org.sbbs.base.pager.impl;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.base.pager.PagerImpl;
import org.sbbs.base.pager.SortOrder;

public class JqgridPagerImpl extends PagerImpl {

	public String getSortNameKey() {
		return "sidx";
	}

	public String getPageNumberKey() {
		return "page";
	}

	public String getSortDirectionKey() {
		return "sord";
	}

	public String getPageSizeKey() {
		return "rows";
	}

	public JqgridPagerImpl(HttpServletRequest request) {
		String sortName = request.getParameter(this.getSortNameKey());
		String sdir = request.getParameter(this.getSortDirectionKey());

		if (sortName != null && !sortName.trim().equalsIgnoreCase("")) {
			this.setSortCriterion(sortName);
			this.setSortDirection(SortOrder.DESCENDING.getValue()
					.equalsIgnoreCase(sdir)
					? SortOrder.DESCENDING
					: SortOrder.ASCENDING);
		} else {
			this.setSortCriterion(null);
		}

		// 设置请求的页码与页面记录条数
		int pageSize = 0;
		String thePage = request.getParameter(this.getPageNumberKey());
		String ps = request.getParameter(this.getPageSizeKey());
		if (ps != null && ps.trim().length() > 0) {
			pageSize = new Integer(ps).intValue();
		} else {
			pageSize = this.DEFAULT_PAGE_SIZE;
		}
		this.setPageSize(pageSize);
		// index 是从0开始计数的，通过 getPageNumber方法进行调整
		if (thePage != null) {
			int index = this == null ? 0 : Integer.parseInt(thePage) - 1;
			this.setIndex(index);
		} else {
			this.setIndex(0);
		}
	}
}
